package edu.cmu.isr.dinvenio.ui.views;

import java.util.Enumeration;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.ui.part.ViewPart;

import edu.cmu.isr.dinvenio.Activator;

public class LayoutViewPart extends ViewPart {
	private Composite parent;
	private Group LayoutNamesGroup;
	
	public LayoutViewPart() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		this.parent = parent;
		
		RowLayout l = new RowLayout(SWT.VERTICAL);
		l.fill = true;
		parent.setLayout(l);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
	public void addLayoutNames(Enumeration<String> layoutNames, String initName) {		
		this.clearLayoutNames();
		
		LayoutNamesGroup = new Group(parent, SWT.NONE);
		LayoutNamesGroup.setText("Graph Layouts");		
		LayoutNamesGroup.setLayout(new RowLayout(SWT.VERTICAL));
		
		if (layoutNames != null){
			for (Enumeration<String> e = layoutNames ; e.hasMoreElements() ;) {    
		        String lName = e.nextElement();
		        
				final Button b = new Button(LayoutNamesGroup, SWT.RADIO);
				b.setText(lName);
				if (lName.equals(initName)){b.setSelection(true);}
				b.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent event) {										
						if (b.getSelection() == true) {
							Activator.myController.setGraphLayout(b.getText());
						}									
					}
				});		        
			}
		}
		refreshView();
	}
	
	private void clearLayoutNames() {
		if (LayoutNamesGroup != null){
			LayoutNamesGroup.dispose();
		}
	}
	
	public void refreshView(){
		parent.layout(true);
	}

}
